<html>
<head><meta charset="utf-8"><title>UB testcase · project-ffi-unwind · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/index.html">project-ffi-unwind</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html">UB testcase</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="229810993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/229810993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#229810993">(Mar 11 2021 at 08:19)</a>:</h4>
<p>Could someone please confirm that <a href="https://github.com/rust-lang/miri/issues/1740">https://github.com/rust-lang/miri/issues/1740</a> is indeed an example of UB under the ffi-unwind RFC, and as such should be detected by Miri?</p>



<a name="229840236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/229840236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#229840236">(Mar 11 2021 at 12:29)</a>:</h4>
<p>Hm, can you clarify which example? Unwinding from "C" functions indeed will be UB, I believe- unwind(allowed) I wouldn't expect to change that, and would expect for it to go away soon.</p>



<a name="229843738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/229843738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#229843738">(Mar 11 2021 at 13:00)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> <code>#[unwind(allowed)]</code> makes unwinding not UB. It has the same effect as <code>extern "C-unwind"</code> but will be phased out in favor of the latter.</p>



<a name="229846520"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/229846520" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#229846520">(Mar 11 2021 at 13:21)</a>:</h4>
<p>Hm, do we store it into metadata? For some reason I didn't recall us doing so, but good to know if it does.</p>



<a name="230152665"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230152665" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230152665">(Mar 13 2021 at 10:05)</a>:</h4>
<p><span class="user-mention silent" data-user-id="143274">Amanieu</span> <a href="#narrow/stream/210922-project-ffi-unwind/topic/UB.20testcase/near/229843738">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <code>#[unwind(allowed)]</code> makes unwinding not UB.</p>
</blockquote>
<p>I see, thanks.</p>



<a name="230152672"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230152672" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230152672">(Mar 13 2021 at 10:05)</a>:</h4>
<p>Hm, but it still seems to be UB with <code>-C panic=abort</code></p>



<a name="230170493"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230170493" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230170493">(Mar 13 2021 at 15:48)</a>:</h4>
<p>Correct.</p>



<a name="230173332"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230173332" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230173332">(Mar 13 2021 at 16:25)</a>:</h4>
<p>If that is intended to stay that way, we should probably update the unwinding UB in <a href="https://doc.rust-lang.org/reference/behavior-considered-undefined.html">https://doc.rust-lang.org/reference/behavior-considered-undefined.html</a></p>



<a name="230173527"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230173527" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230173527">(Mar 13 2021 at 16:29)</a>:</h4>
<p>looks like the ways to cause UB are</p>
<ul>
<li>calling a function with a on-unwinding caller ABI, and that fn unwinds (this is possible even with the abort-on-panic shims when function pointers are cast around)</li>
<li>calling any unwinding function with <code>-C panic=abort</code></li>
</ul>



<a name="230173536"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230173536" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230173536">(Mar 13 2021 at 16:29)</a>:</h4>
<p>doesn't the latter make things like the Rust lua wrappers unsound with <code>-C panic=abort</code>? That seems like a footgun</p>



<a name="230173805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230173805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230173805">(Mar 13 2021 at 16:33)</a>:</h4>
<p>Lua wrappers are force-unwound or longjmp'd over, I think.</p>



<a name="230173862"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230173862" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230173862">(Mar 13 2021 at 16:34)</a>:</h4>
<p>Force-unwinding should not be UB, I think, but I need to check the RFC again to be sure that's explicit.</p>



<a name="230173953"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230173953" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230173953">(Mar 13 2021 at 16:35)</a>:</h4>
<p>Is unwinding in <code>panic=abort</code> possible in pure Rust, or does it require FFI somewhere?</p>



<a name="230174307"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230174307" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230174307">(Mar 13 2021 at 16:42)</a>:</h4>
<p>(I have been assuming the latter)</p>



<a name="230179818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230179818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230179818">(Mar 13 2021 at 18:20)</a>:</h4>
<p>okay maybe lua is a bad example</p>



<a name="230179830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230179830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230179830">(Mar 13 2021 at 18:21)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120076">BatmanAoD (Kyle Strand)</span> <a href="#narrow/stream/210922-project-ffi-unwind/topic/UB.20testcase/near/230173953">said</a>:</p>
<blockquote>
<p>Is unwinding in <code>panic=abort</code> possible in pure Rust, or does it require FFI somewhere?</p>
</blockquote>
<p>it requires FFI, but that's the point -- how do I write a safe Rust wrapper around FFI that can unwind?</p>



<a name="230179863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230179863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230179863">(Mar 13 2021 at 18:21)</a>:</h4>
<p>looks like that is impossible since if someone uses my safe wrapper with <code>-C panic=abort</code>, we have UB</p>



<a name="230184051"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230184051" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230184051">(Mar 13 2021 at 19:36)</a>:</h4>
<p>The plan is for <code>extern "C-unwind"</code> to catch foreign unwinds and turn them into aborts when building with <code>panic=abort</code>.</p>



<a name="230184068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230184068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230184068">(Mar 13 2021 at 19:36)</a>:</h4>
<p>I'm not sure what the current behavior is.</p>



<a name="230185489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230185489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230185489">(Mar 13 2021 at 20:02)</a>:</h4>
<p>That should indeed be the current behavior; the only problem is that the ABI is behind a feature gate.</p>



<a name="230246784"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230246784" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230246784">(Mar 14 2021 at 15:56)</a>:</h4>
<p><span class="user-mention silent" data-user-id="143274">Amanieu</span> <a href="#narrow/stream/210922-project-ffi-unwind/topic/UB.20testcase/near/230184051">said</a>:</p>
<blockquote>
<p>The plan is for <code>extern "C-unwind"</code> to catch foreign unwinds and turn them into aborts when building with <code>panic=abort</code>.</p>
</blockquote>
<p>opened a soundness issue for that <a href="https://github.com/rust-lang/rust/issues/83116">https://github.com/rust-lang/rust/issues/83116</a></p>



<a name="230246797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230246797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230246797">(Mar 14 2021 at 15:56)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120076">BatmanAoD (Kyle Strand)</span> <a href="#narrow/stream/210922-project-ffi-unwind/topic/UB.20testcase/near/230185489">said</a>:</p>
<blockquote>
<p>That should indeed be the current behavior; the only problem is that the ABI is behind a feature gate.</p>
</blockquote>
<p>I cannot see any trace of such an abort-to-unwind wrapper in the LLVM IR for that issue, am I looking at the wrong place?</p>



<a name="230257531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/230257531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#230257531">(Mar 14 2021 at 17:57)</a>:</h4>
<p>That appears to be an oversight, then. <span class="user-mention" data-user-id="257364">@Katelyn Martin</span>, we won't be able to stabilize <code>"C-unwind"</code> without that feature, but I don't know how to implement it or whether we have anything similar already in the compiler.</p>



<a name="231140848"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231140848" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231140848">(Mar 20 2021 at 11:32)</a>:</h4>
<p>Wait, I'm trying to remember now -- were we going to put those wrappers <em>all the time</em>? Wasn't it the case that you can force-unwind over a frame with no dtors, at least?</p>



<a name="231140860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231140860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231140860">(Mar 20 2021 at 11:32)</a>:</h4>
<p>presumably the RFC has the details though</p>



<a name="231140916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231140916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231140916">(Mar 20 2021 at 11:33)</a>:</h4>
<p>It should be possible to abort on regular unwinds, but still allow forced-unwinds I think.</p>



<a name="231146397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231146397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231146397">(Mar 20 2021 at 13:24)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/210922-project-ffi-unwind/topic/UB.20testcase/near/231140848">said</a>:</p>
<blockquote>
<p>Wait, I'm trying to remember now -- were we going to put those wrappers <em>all the time</em>? Wasn't it the case that you can force-unwind over a frame with no dtors, at least?</p>
</blockquote>
<p>What are the LLVM rules for force-unwinding out of a function call marked <code>nounwind</code>?<br>
If LLVM says this is UB, then we have the same UB in Rust, since with panic=abort we add <code>nounwind</code> basically everywhere.</p>



<a name="231150354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231150354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231150354">(Mar 20 2021 at 14:43)</a>:</h4>
<p>C code <code>longjmp</code>s over <code>nounwind</code> frames all the time. So I assume it is allowed.</p>



<a name="231150356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231150356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231150356">(Mar 20 2021 at 14:43)</a>:</h4>
<p>force-unwind is longjmp, right?</p>



<a name="231150376"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231150376" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231150376">(Mar 20 2021 at 14:43)</a>:</h4>
<p>Yeah, I believe C++ noexcept gets turned into nouwind as well, so same thing there.</p>



<a name="231150707"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231150707" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231150707">(Mar 20 2021 at 14:49)</a>:</h4>
<p><a href="https://godbolt.org/z/597Psh">https://godbolt.org/z/597Psh</a><br>
According to this CE build, it does. And in fact, <code>longjmp</code>itself is <code>nouwind</code>, so I think its safe to assume that <code>longjmp</code>ing out of a <code>nounwind</code> function is not UB in llvm.</p>



<a name="231153656"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231153656" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231153656">(Mar 20 2021 at 15:45)</a>:</h4>
<p><code>longjmp</code> is only implemented as force-unwind on Windows. On all other platforms it simply restores all registers and the signal handler state. Force-unwinding uses the DWARF/SEH unwinding tables to unwind the stack and run destructors like with regular unwinding, except that it can't be catched. <code>nounwind</code> would cause the compiler to omit the destructors, but it will still emit the unwinding tables necessary for force-unwinding I think.</p>



<a name="231153717"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231153717" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231153717">(Mar 20 2021 at 15:46)</a>:</h4>
<p><a href="https://stackoverflow.com/questions/14268080/cancelling-a-thread-that-has-a-mutex-locked-does-not-unlock-the-mutex">https://stackoverflow.com/questions/14268080/cancelling-a-thread-that-has-a-mutex-locked-does-not-unlock-the-mutex</a></p>
<blockquote>
<p>On GNU/Linux pthread_cancel is implemented with a special exception of type __cxxabi::__forced_unwind, so when a thread is cancelled an exception is thrown and the stack is unwound. If a mutex is locked by an RAII type then its destructor will be guaranteed to run if the stack is unwound by a __forced_unwind exception.</p>
</blockquote>



<a name="231156023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231156023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hyd-dev <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231156023">(Mar 20 2021 at 16:30)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/210922-project-ffi-unwind/topic/UB.20testcase/near/231153656">said</a>:</p>
<blockquote>
<p><code>nounwind</code> would cause the compiler to omit the destructors, but it will still emit the unwinding tables necessary for force-unwinding I think.</p>
</blockquote>
<p>cc <a href="https://github.com/rust-lang/rust/issues/69984">#69984</a></p>



<a name="231157886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231157886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hyd-dev <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231157886">(Mar 20 2021 at 17:03)</a>:</h4>
<p><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1302078">https://bugzilla.mozilla.org/show_bug.cgi?id=1302078</a>, the link mentioned in the comment around <code>must_emit_unwind_tables</code> in <a href="https://github.com/rust-lang/rust/issues/69984">#69984</a>, looks also relevant.</p>



<a name="231160005"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231160005" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hyd-dev <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231160005">(Mar 20 2021 at 17:40)</a>:</h4>
<p>(deleted)</p>



<a name="231160886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231160886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231160886">(Mar 20 2021 at 17:55)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/210922-project-ffi-unwind/topic/UB.20testcase/near/231153717">said</a>:</p>
<blockquote>
<p><a href="https://stackoverflow.com/questions/14268080/cancelling-a-thread-that-has-a-mutex-locked-does-not-unlock-the-mutex">https://stackoverflow.com/questions/14268080/cancelling-a-thread-that-has-a-mutex-locked-does-not-unlock-the-mutex</a></p>
<blockquote>
<p>On GNU/Linux pthread_cancel is implemented with a special exception of type __cxxabi::__forced_unwind, so when a thread is cancelled an exception is thrown and the stack is unwound. If a mutex is locked by an RAII type then its destructor will be guaranteed to run if the stack is unwound by a __forced_unwind exception.</p>
</blockquote>
</blockquote>
<p>if RAII destructors like mutex are run on this kind of unwinding, then it seems to be different from what was called "forced unwinding" in the Rust FFI WG?</p>



<a name="231161688"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231161688" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231161688">(Mar 20 2021 at 18:08)</a>:</h4>
<p>Rust's interaction with forced unwinding is not specified; that's what the longjmp/cancelable discussion is about.</p>
<p>Per the <code>"C-unwind"</code> RFC, forced unwinding over non-POFs will <em>never</em> be well defined, so we don't need to worry about that case. We only need to ensure that forced unwinding over POFs is safe (and in the future, we may restrict that to POFs with a specific annotation).</p>



<a name="231161925"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231161925" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231161925">(Mar 20 2021 at 18:12)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/210922-project-ffi-unwind/topic/UB.20testcase/near/231140848">said</a>:</p>
<blockquote>
<p>Wait, I'm trying to remember now -- were we going to put those wrappers <em>all the time</em>? Wasn't it the case that you can force-unwind over a frame with no dtors, at least?</p>
</blockquote>
<p>The wrappers should explicitly not trap on forced-unwinding.</p>



<a name="231161975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231161975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231161975">(Mar 20 2021 at 18:13)</a>:</h4>
<p>But yes, when compiling with <code>panic=abort</code>, all _invocations_ of <code>"C-unwind"</code> functions would need to use such a wrapper.</p>



<a name="231163000"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231163000" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231163000">(Mar 20 2021 at 18:31)</a>:</h4>
<p>In case of rust force unwinding on arm ignores cleanup: <a href="https://github.com/rust-lang/rust/blob/41b315a470d583f6446599984ff9ad3bd61012b2/library/panic_unwind/src/gcc.rs#L148-L150">https://github.com/rust-lang/rust/blob/41b315a470d583f6446599984ff9ad3bd61012b2/library/panic_unwind/src/gcc.rs#L148-L150</a><br>
On other platforms I do think it runs destructors when the unwind table generated by LLVM contains an entry for the current call: <a href="https://github.com/rust-lang/rust/blob/41b315a470d583f6446599984ff9ad3bd61012b2/library/panic_unwind/src/gcc.rs#L226-L260">https://github.com/rust-lang/rust/blob/41b315a470d583f6446599984ff9ad3bd61012b2/library/panic_unwind/src/gcc.rs#L226-L260</a></p>



<a name="231163278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/UB%20testcase/near/231163278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/UB.20testcase.html#231163278">(Mar 20 2021 at 18:35)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> I don't think it matters. As I said, Rust's behavior is explicitly not specified for force unwinding over frames with cleanup, and we have no plans to specify it in the future.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>